//
//  DDropSegmentTableViewCell.swift
//  Drop
//
//  Created by hushaohua on 2018/2/26.
//  Copyright © 2018年 init. All rights reserved.
//

import UIKit

class DDropTrailTableViewCell: UITableViewCell {
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);
        self.contentView.addSubview(self.lineView);
        self.addSubviewConstraints();
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        // Configure the view for the selected state
    }
    
    lazy var lineView:UIView = {
        var view:UIView = UIView(frame: .zero);
        view.translatesAutoresizingMaskIntoConstraints = false;
        view.backgroundColor = UIColor.clear;
        for index in 0..<3 {
            let layer = self.dotLayer(CGRect(x:0,y:(4 + index * 10),width:6,height:6));
            view.layer.addSublayer(layer);
        }
        return view;
    }();
    
    func dotLayer(_ frame:CGRect) -> CALayer{
        let layer:CALayer = CALayer();
        layer.backgroundColor = UIColor.darkGray.cgColor;
        layer.frame = frame;
        layer.cornerRadius = 3;
        layer.masksToBounds = true;
        return layer;
    }
    
    func addSubviewConstraints() -> Void{
        let views:[String : UIView] = ["line" : self.lineView];
        var costraints = [NSLayoutConstraint]();
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[line]-0-|", options: [.directionLeadingToTrailing], metrics:nil, views: views);
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-left-[line(6)]", options: .directionLeadingToTrailing, metrics: ["left" : 142], views: views);
        self.contentView.addConstraints(costraints);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class DDropSegmentTableViewCell: UITableViewCell {

    static let normalReuseId:String = "normal";
    static let closeNextReuseId:String = "closeToNext";
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier);
        
        self.endDotView = self.dateDotView();
        self.endDateLabel = self.dateLabel();
        self.contentView.addSubview(self.endDotView!);
        self.contentView.addSubview(self.endDateLabel!);
        self.contentView.addSubview(self.lineView);
        self.contentView.addSubview(self.durationLabel);
        if (reuseIdentifier != DDropSegmentTableViewCell.closeNextReuseId){
            self.beginDotView = self.dateDotView();
            self.beginDateLabel = self.dateLabel();
            self.contentView.addSubview(self.beginDotView!);
            self.contentView.addSubview(self.beginDateLabel!);
        }
        self.addSubviewConstraints();
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        
        // Configure the view for the selected state
    }
    
    lazy var lineView:UIView = {
        var view:UIView = UIView(frame: .zero);
        view.translatesAutoresizingMaskIntoConstraints = false;
        view.backgroundColor = UIColor.darkGray;
//        let layer = CAShapeLayer();
//        let path = UIBezierPath();
//        path.move(to: CGPoint(x:5,y:0))
//        path.addLine(to: CGPoint(x:0,y:10))
//        path.addLine(to: CGPoint(x:2,y:10))
//        path.addLine(to: CGPoint(x:2,y:32))
//        path.addLine(to: CGPoint(x:8,y:32))
//        path.addLine(to: CGPoint(x:8,y:10))
//        path.addLine(to: CGPoint(x:10,y:10))
//        path.close();
//        layer.path = path.cgPath;
//        view.layer.mask = layer;
        return view;
    }();
    
    var beginDotView:UIView?;
    var endDotView:UIView?;
    func dateDotView() -> UIView{
        let view:UIView = UIView(frame: .zero);
        view.layer.cornerRadius = 5;
        view.layer.masksToBounds = true;
        view.backgroundColor = UIColor.darkGray;
        view.translatesAutoresizingMaskIntoConstraints = false;
        return view;
    }
    var beginDateLabel:UILabel?;
    var endDateLabel:UILabel?;
    func dateLabel() -> UILabel{
        let label:UILabel = UILabel(frame: .zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor.gray;
        label.font = UIFont.systemFont(ofSize: 16, weight: .medium);
        label.textAlignment = .left;
//        label.backgroundColor = UIColor.yellow;
        return label;
    }
    
    lazy var durationLabel:UILabel = {
        var label:UILabel = UILabel(frame:.zero);
        label.translatesAutoresizingMaskIntoConstraints = false;
        label.textColor = UIColor.gray;
        label.font = UIFont.systemFont(ofSize: 14, weight: .light);
        label.textAlignment = .right;
//        let layer:CALayer = CALayer();
//        layer.backgroundColor = UIColor(white:0.95,alpha:0.9).cgColor;
//        layer.frame = CGRect(x:0,y:10,width:UIScreen.main.bounds.width - 102 - 6 - 40 - 50,height:1);
//        label.layer.addSublayer(layer)
        return label;
    }();
    
    func addSubviewConstraints() -> Void{
        var views:[String : UIView] = ["line" : self.lineView, "endDate" : self.endDateLabel!, "endDot" : self.endDotView!, "duration" : self.durationLabel];
        var costraints = [NSLayoutConstraint]();
        if (self.beginDateLabel != nil && self.beginDotView != nil){
            views["beginDot"] = self.beginDotView!;
            views["beginDate"] = self.beginDateLabel!;
            costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[beginDot(==10)]-space-[line]-space-[endDot(==10)]-0-|", options: [.alignAllCenterX], metrics: ["space" : 0], views: views);
            costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:[beginDate(20)]-0-|", options: [.directionLeadingToTrailing], metrics: nil, views: views);
            costraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-margin-[beginDot(==10)]-10-[beginDate]-0-|", options: .directionLeadingToTrailing, metrics: ["margin" : 140], views: views);
        }else{
            costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[endDot(==10)]-space-[line]-0-|", options: [.alignAllCenterX], metrics: ["space" : 0], views: views);
        }
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-20-[duration(20)]", options: [.directionLeadingToTrailing], metrics: nil, views: views);
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[duration]-10-[line]", options: .directionLeadingToTrailing, metrics: nil, views: views);
        
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[endDate(20)]", options: [.directionLeadingToTrailing], metrics: nil, views: views);
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-left-[line(6)]", options: .directionLeadingToTrailing, metrics: ["left" : 142], views: views);
        costraints += NSLayoutConstraint.constraints(withVisualFormat: "H:|-margin-[endDot(==10)]-10-[endDate]-0-|", options: .directionLeadingToTrailing, metrics: ["margin" : 140], views: views);
        self.contentView.addConstraints(costraints);
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
